package com.familydoctor.test;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.Session;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.fastjson.JSON;
import com.familydoctor.config.FileConfigProps;
import com.familydoctor.entity.*;
import com.familydoctor.listener.MyExlListener;
import com.familydoctor.listener.ReadAllListener;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: tool
 * @description:
 * @author: tmz
 * @create: 2020-10-12 10:03
 */
@Slf4j
public class ExlTest2 {
    public static void main(String[] args) throws Exception{
        List<Category> category = Db.use().find(Entity.create("Category"), Category.class);
        category.sort(Comparator.comparingInt(Category::getId));
        List<DetailModel> res = new ArrayList<>();
        File exl = new File(FileConfigProps.getFilePath());
        ExcelReader excelReader = null;
        try{
            excelReader = EasyExcel.read(exl).build();
            ReadSheet readSheet1 =
                    EasyExcel.readSheet("第二页内容")
                            .registerReadListener(new MyExlListener(res,category))
                            .head(ContentSheetModel.class).headRowNumber(1).build();
            List<ReadSheet> sheets = new ArrayList<>();
            sheets.add(readSheet1);
            category.forEach(item->{
                sheets.add(EasyExcel.readSheet(item.getName())
                        .registerReadListener(new ReadAllListener(res))
                        .head(TypeSheetModel.class).headRowNumber(1).build());
            });
            excelReader.read(sheets);
        }catch (Exception e){
            e.printStackTrace();
            log.error("异常->{}",e);
        } finally{
            if (excelReader != null) {
                // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
                excelReader.finish();
            }
        }
        List<Exam> insertList = new ArrayList<>();
        res.forEach(item->{
            Exam exam = new Exam();
            exam.setTitle(item.getTitle())
                    .setMd5Value(SecureUtil.md5(item.getTitle()))
                    .setCategory(getCategoryId(category,item.getCategory()))
//                    .setDetail(JSONUtil.toJsonStr(item));
                    .setDetail(JSON.toJSONString(item));
            insertList.add(exam);
        });
        Session session = Session.create();
        try{
            session.beginTransaction();
            List<Entity> collect = insertList.stream().map(e -> Entity.parse(e,true,true)).collect(Collectors.toList());
            session.insert(collect);
            session.commit();
        }catch (Exception e){
            session.quietRollback();
            e.printStackTrace();
        }
    }
    private static Integer getCategoryId(List<Category> category,String cname){
        return category.stream().filter(item-> StrUtil.equals(item.getName(),cname)).collect(Collectors.toList()).get(0).getId();
    }
}
